<template>
  <div class="app-container">

    <div style="text-align: center;margin-bottom: 1%;margin-top: 1%">
      <!--查询表单-->
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="objectQuery.title" placeholder="标题"/>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
          <el-button type="default" icon="el-icon-delete" @click="resetData()">清空</el-button>
        </el-form-item>

        <el-form-item>
          <el-button type="success" icon="el-icon-circle-plus-outline" @click="SaveOrUpdate(0)">添加</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格 -->
    <el-table :data="list" border stripe style="padding: 30px 0; text-align: center;margin-right: 1%;margin-left: 1%">
      <el-table-column label="封面" width="80" align="center">
        <template slot-scope="scope">
          <img :src="scope.row.cover" width="100%">
        </template>
      </el-table-column>
      <el-table-column prop="subjectTitle" label="分类标题" width="200" align="center"/>
      <el-table-column prop="title" label="课程标题" width="250" align="center"/>
      <el-table-column prop="price" label="价格" width="100" align="center"/>
      <el-table-column prop="description" label="课程简介" width="100" align="center"/>
      <el-table-column prop="gmtCreate" label="创建时间" width="100" align="center"/>
      <el-table-column prop="status" label="发布状态" width="100" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === false" type="danger">未发布</el-tag>
          <el-tag v-if="scope.row.status === true" type="success">已发布</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <el-button type="success" size="mini" icon="el-icon-error" @click="publish(scope.row.id, true)"
                     v-if="scope.row.status === false">发布
          </el-button>
          <el-button type="danger" size="mini" icon="el-icon-success" @click="publish(scope.row.id, false)"
                     v-if="scope.row.status === true">下架
          </el-button>

          <el-button type="primary" size="mini" icon="el-icon-edit" @click="SaveOrUpdate(scope.row.id)">编辑
          </el-button>

          <el-button type="danger" size="mini" icon="el-icon-error" @click="remove(scope.row.id)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20]"
      style="padding: 30px 0; text-align: center"
      layout="total, sizes, prev, pager, next, jumper"
      @current-change="fetchData"
      @size-change="changePageSize"/>
  </div>
</template>

<script>
  import courseApi from '@/api/course'

  export default {
    // 页内数据
    data() {
      return {
        list: [],
        page: 1,
        limit: 5,
        total: 0,
        objectQuery: {}
      }
    },

    //初始化
    created() {
      this.fetchData()
    },

    methods: {
      //获取数据
      fetchData(page = 1) {
        this.page = page
        courseApi.pageQueryCourse(this.page, this.limit, this.objectQuery)
          .then(response => {
            this.list = response.data.rows
            this.total = response.data.total
          })
          .catch(error => {
            console.log(error)
          })
      },

      // 改变每页记录数
      changePageSize(size) {
        this.limit = size
        this.fetchData()
      },

      // 重置表单
      resetData() {
        this.objectQuery = {}
        this.fetchData()
      },

      // 修改禁用状态
      publish(id, status) {
        this.objectQuery.id = id
        this.objectQuery.status = status
        courseApi.updateCourse(this.objectQuery)
          .then(response => {
            this.objectQuery = {}
            this.fetchData()
          })
          .catch(error => {
            console.log(error)
          })
      },

      // 删除记录
      remove(id) {
        // 询问是否删除
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          return courseApi.deleteCourse(id)
        }).then(response => {
          // 刷新页面
          this.fetchData()
          // 弹出成功提示
          this.$message({
            message: response.message,
            type: 'success'
          })
        })
      },

      SaveOrUpdate(id) {
        this.$router.push({ path: '/course/courseInfo/' + id })
      }
    }
  }
</script>
